package pl.r1103.autostrada;
/**
 * 
 */

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.math.BigDecimal;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;

import pl.r1103.autostrada.exceptions.NoSuchTicketException;

/** 
 * @author Stazi
 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class CoreServiceImpl extends JFrame implements CoreService{
	

	private static final long serialVersionUID = 1L;
	private JPanel panel = null;
	private JTextArea textArea = null;
	private Random random = null;

	public CoreServiceImpl(){
		super();
		initialize();
	}
	

	public static void main(String[] args) {
		if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
		try {
			CoreService service = new CoreServiceImpl();
			
			String name = "CoreService";
			CoreService stub = (CoreService)UnicastRemoteObject.exportObject(service, 0);
			Registry registry = LocateRegistry.getRegistry();
            registry.rebind(name, stub);
            System.out.println("CoreService bound");
		} catch (RemoteException e) {
			System.out.println("CoreService exception");
			e.printStackTrace();
		}
	}
	
	private void initialize() {
		if (terminals == null)
			terminals = new HashSet<Terminal>();
		if (gates == null)
			gates = new HashSet<Gate>();
		if (tickets == null)
			tickets = new HashMap<Long,Ticket>();
		if (random == null){
			random = new Random(System.currentTimeMillis());
		}
		
		 //this.setResizable(false);
		 this.setTitle("CoreService");
		 this.setBounds(300, 300, 200, 200);
		 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 this.setVisible(true);
		 
		 
		 this.setContentPane(getJContentPane());
		// this.getContentPane().setLayout(new BorderLayout());
		 //this.getContentPane().add(getJScrollPane(),BorderLayout.CENTER);
		 //this.getContentPane().add(new JButton("przycik"),BorderLayout.SOUTH);

		
	}
	
	private JPanel getJContentPane() {
		if (panel == null){
			panel = new JPanel();
			panel.setLayout(new BorderLayout());
			panel.add(getJTextArea(), BorderLayout.CENTER);
			panel.add(new JLabel("Log"),BorderLayout.NORTH);
			//panel.add(getJTextArea());
		}
		return panel ;
	}

	private JTextArea getJTextArea() {
		if (textArea == null){
			textArea = new JTextArea();
			textArea.setEnabled(false);
			textArea.setAutoscrolls(true);
		}
		return textArea;
	}

	/** 
	 * @uml.annotations for <code>terminals</code>
	 *     collection_type="Terminal"
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	private Set<Terminal> terminals;

	/** 
	 * @return the terminals
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public Set<Terminal> getTerminals() {
		// begin-user-code
		return terminals;
		// end-user-code
	}

	/** 
	 * @param theTerminals the terminals to set
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void setTerminals(Set<Terminal> theTerminals) {
		// begin-user-code
		terminals = theTerminals;
		// end-user-code
	}

	/** 
	 * @uml.annotations for <code>gates</code>
	 *     collection_type="Gate"
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	private Set<Gate> gates;

	/** 
	 * @return the gates
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public Set<Gate> getGates() {
		// begin-user-code
		return gates;
		// end-user-code
	}

	/** 
	 * @param theGates the gates to set
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void setGates(Set<Gate> theGates) {
		// begin-user-code
		gates = theGates;
		// end-user-code
	}

	/** 
	 * @uml.annotations for <code>tickets</code>
	 *     collection_type="Ticket"
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	private Map<Long,Ticket> tickets;

	/** 
	 * @return the tickets
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public Map<Long,Ticket> getTickets() {
		// begin-user-code
		return tickets;
		// end-user-code
	}

	/** 
	 * @param theTickets the tickets to set
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void setTickets(Map<Long,Ticket> theTickets) {
		// begin-user-code
		tickets = theTickets;
		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void addNewTerminal() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void getTerminalList() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void deleteTerminal() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void authenticate() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void LogOn() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void checkSession() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void changeGateName() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void defineNewFine() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void getStatistics() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public Long registerTicket(Ticket arg) throws RemoteException{
		arg.setID(Math.abs(random.nextLong()%2048));
		while (tickets.containsKey(arg.getID()))
			arg.setID(Math.abs(random.nextLong()%2048));
		
		
		if (tickets.put(arg.getID(), arg)==null){
			textArea.append("New ticket added. ID:" + String.valueOf(arg.getID()) + "\n");
			return arg.getID();
		}
		else
			return null;
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void getGatesList() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void resetStatistics() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void setFaresMatrix() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void getFaresMatrix() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void addNewGate() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public BigDecimal getPriceForTicket(Long ticketID) throws NoSuchTicketException{
		if (!tickets.containsKey(ticketID))
			throw new NoSuchTicketException();
		
		return new BigDecimal((ticketID*234)%35);
		
	}

	/** 
	 * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void registerPayment() {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}
}